//package com.ftf.tp.user.infrastructure.config;
//
//import org.apache.http.HttpHost;
//import org.elasticsearch.client.RestClient;
//import org.elasticsearch.client.RestClientBuilder;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//import java.util.ArrayList;
//import java.util.List;
//
///**
// * es配置
// *
// * @author tanghao
// */
//@Configuration
//public class ElasticsearchConfig {
//
//    @Autowired(required = false)
//    private ElasticsearchParams elasticsearchParams;
//
//    /**
//     * 当前es相关的配置存在则实例化RestHighLevelClient,如果不存在则不实例化RestHighLevelClient
//     */
//    @Bean
//    @ConditionalOnBean(value = ElasticsearchParams.class)
//    public RestHighLevelClient restHighLevelClient() {
//
//        //es地址，以逗号分隔
//        String nodes = elasticsearchParams.getAddress();
//        nodes = nodes.contains("http://") ? nodes.replace("http://", "") : nodes;
//        //es密码
//        String scheme = elasticsearchParams.getScheme();
//        List<HttpHost> httpHostList = new ArrayList<>();
//        //拆分es地址
//        for (String address : nodes.split(",")) {
//            int index = address.lastIndexOf(":");
//            httpHostList.add(new HttpHost(address.substring(0, index), Integer.parseInt(address.substring(index + 1)), scheme));
//        }
//        //转换成 HttpHost 数组
//        HttpHost[] httpHosts = httpHostList.toArray(new HttpHost[httpHostList.size()]);
//
//        //构建连接对象
//        RestClientBuilder builder = RestClient.builder(httpHosts);
//
//        // 异步连接延时配置
//        builder.setRequestConfigCallback(requestConfigBuilder -> {
//            requestConfigBuilder.setConnectTimeout(elasticsearchParams.getConnectTimeout());
//            requestConfigBuilder.setSocketTimeout(elasticsearchParams.getSocketTimeout());
//            requestConfigBuilder.setConnectionRequestTimeout(elasticsearchParams.getConnectionRequestTimeout());
//            return requestConfigBuilder;
//        });
//
//        // 异步连接数配置
//        builder.setHttpClientConfigCallback(httpClientBuilder -> {
//            httpClientBuilder.setMaxConnTotal(elasticsearchParams.getMaxConnectNum());
//            httpClientBuilder.setMaxConnPerRoute(elasticsearchParams.getMaxConnectPerRoute());
//            return httpClientBuilder;
//        });
//
//        return new RestHighLevelClient(builder);
//    }
//
//
//}
//
//
